package com.t2two.example.projects;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 * @description: 返回值封装类
 * @company: 智蓝图汇
 * @author: bruce lan/00001
 * @date: 2021/5/26 21:50
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private Integer code;

    private String message;

    private T result;

    public static ResponseResult success() {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.OK.getCode());
        rr.setMessage(StatusCode.OK.getDesc());
        return rr;
    }

    public static ResponseResult success(Object result, String message) {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.OK.getCode());
        rr.setMessage(message);
        rr.setResult(result);
        return rr;
    }

    public static ResponseResult success(Object result) {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.OK.getCode());
        rr.setMessage(StatusCode.OK.getDesc());
        rr.setResult(result);
        return rr;
    }

    public static ResponseResult fail(String message) {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.ERROR.getCode());
        rr.setMessage(message);
        return rr;
    }

    public static ResponseResult fail(Object result) {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.ERROR.getCode());
        rr.setMessage(StatusCode.ERROR.getDesc());
        rr.setResult(result);
        return rr;
    }

    public static ResponseResult fail(Integer errCode, String errMessage) {
        ResponseResult rr = new ResponseResult();
        rr.setCode(errCode);
        rr.setMessage(errMessage);
        return rr;
    }

    public static ResponseResult paramError() {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.PARAM_ERROR.getCode());
        rr.setMessage(StatusCode.PARAM_ERROR.getDesc());
        return rr;
    }

    /**
     * @author     : bruce lan/00001
     * @description: 数据不存在
     * @date       : 2021/7/12 19:28
     * @return
     */
    public static ResponseResult dnaError() {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.DNA.getCode());
        rr.setMessage(StatusCode.DNA.getDesc());
        return rr;
    }

    /**
     * @author     : bruce lan/00001
     * @description: 数据库错误
     * @date       : 2021/7/12 20:19
     * @return
     */
    public static ResponseResult dbError() {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.DB_ERROR.getCode());
        rr.setMessage(StatusCode.DB_ERROR.getDesc());
        return rr;
    }

    public static ResponseResult returnSuccessOrDbError(int ret) {
        if (ret > 0) {
            return success();
        }
        return dbError();
    }

    public static ResponseResult authFail() {
        ResponseResult rr = new ResponseResult();
        rr.setCode(StatusCode.AUTHEN_FAILED.getCode());
        rr.setMessage(StatusCode.AUTHEN_FAILED.getDesc());
        return rr;
    }
}
